<?php


namespace App\Http\Controllers\Manage\System\Dictionary;


use App\Http\Controllers\Controller;
use App\Http\Requests\Manage\System\Dictionary\Country\StoreRequest;
use App\Http\Requests\Manage\System\Dictionary\Country\UpdateRequest;
use App\Http\Searchs\Manage\System\Dictionary\CountrySearch;
use App\Repositories\Country;

class CountryController extends Controller
{
    /**
     * @var Country
     * @author luffyzhao@vip.126.com
     */
    private $country;

    /**
     * CountryController constructor.
     * @param Country $country
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Country $country)
    {
        $this->country = $country;
    }

    /**
     * @param CountrySearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(CountrySearch $search)
    {
        return $this->response(
            $this->country->paginate($search->toArray(), request()->input('per_page'))
        );
    }

    /**
     * @param \App\Http\Requests\System\Dictionary\Currency\StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request){
        return $this->response(
            $this->country->create($request->only(['code', 'name']))
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function edit($id){
        return $this->response(
            $this->country->find($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id){
        return $this->response(
            $this->country->update($id, $request->only(['name']))
        );
    }
}